package com.miplus.ccrm.core.service.conf;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.core.controller.vo.conf.WfCaseProcReqVo;
import com.miplus.ccrm.core.controller.vo.conf.WfCaseProcResVo;
import com.miplus.ccrm.core.pojo.WfActions;
import com.miplus.ccrm.core.pojo.WfCaseactions;
import com.miplus.ccrm.core.mapper.WfCaseactionsMapper;
import com.miplus.ccrm.core.pojo.WfCases;
import com.miplus.ccrm.core.pojo.WfSteps;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author pc
* @description 针对表【wf_caseactions】的数据库操作Service实现
* @createDate 2024-04-04 22:11:21
*/
@Service
public class WfCaseactionsServiceImpl extends MPJBaseServiceImpl<WfCaseactionsMapper, WfCaseactions>
    implements WfCaseactionsService{
    @Override
    public int wfActionAdd(String $usrid,Integer wfcaseid,Integer stepid)
    {
        WfCaseactions wfCaseactions=new WfCaseactions();
        wfCaseactions.setCaseid(wfcaseid);
        wfCaseactions.setStepid(stepid);
        wfCaseactions.setCrusr($usrid);
        wfCaseactions.setCrdt(new Date());

        baseMapper.insert(wfCaseactions);

        return wfCaseactions.getCaseactid();
    }

    @Override
    public int wfActionAdd(String $usrid,Integer wfcaseid,Integer stepid,Integer precaseactid)
    {
        WfCaseactions wfCaseactions=new WfCaseactions();
        wfCaseactions.setCaseid(wfcaseid);
        wfCaseactions.setStepid(stepid);
        wfCaseactions.setPrecaseactid(precaseactid);
        wfCaseactions.setCrusr($usrid);
        wfCaseactions.setCrdt(new Date());

        baseMapper.insert(wfCaseactions);

        return wfCaseactions.getCaseactid();
    }

    @Override
    public int wfActionUpdate(String $usrid,Integer caseactid,Integer actionid,String remark)
    {
        WfCaseactions wfCaseactions=new WfCaseactions();
        wfCaseactions.setActionid(actionid);
        wfCaseactions.setActiontype("1");
        wfCaseactions.setNote(remark);
        wfCaseactions.setActionusr($usrid);
        wfCaseactions.setActiondt(new Date());

        int ret=baseMapper.update(wfCaseactions,
                new QueryWrapper<WfCaseactions>()
                        .eq("caseactid",caseactid)
                        .isNull("actionid")
        );

        return ret;
    }

    @Override
    public Integer getWfCaseID(Integer caseactid)
    {
        WfCaseactions f=baseMapper.selectById(caseactid);
        if(f==null) return 0;
        return f.getCaseid();
    }

    @Override
    public List<WfCaseProcResVo> wfProcessList(WfCaseProcReqVo paras)
    {
        return selectJoinList(WfCaseProcResVo.class,
                new MPJLambdaWrapper<WfCaseactions>()
                        .selectAs(WfCaseactions::getCaseactid,WfCaseProcResVo::getId)
                        .selectAs(WfSteps::getName,WfCaseProcResVo::getStepname)
                        .selectAs("fun_getusername(t.actionusr)",WfCaseProcResVo::getActionusername)
                        .selectAs("wa.name",WfCaseProcResVo::getActionname)
                        .innerJoin(WfCases.class,"wc",WfCases::getCaseid,WfCaseactions::getCaseid)
                        .innerJoin(WfSteps.class,"ws",WfSteps::getStepid,WfCaseactions::getStepid)
                        .innerJoin(WfActions.class,"wa",WfActions::getActionid,WfCaseactions::getActionid)
                        .select(WfCaseactions::getCaseactid,WfCaseactions::getStepid,WfCaseactions::getActionusr,WfCaseactions::getActionid,WfCaseactions::getNote,WfCaseactions::getActiondt)
                        .eq(WfCases::getOriginalid,paras.getOriginalid())
                        .eq(WfCases::getWfid,paras.getWfid())
                        .orderByAsc(WfCaseactions::getCaseactid)
        );
    }
}




